require 'yaml'

module Depac
 module CBT
  class EnvironmentConfig
    DEP_DATA="deployment_data"
    WS_PUB_KEY_PATH='ws_data/workstation-key.pub'
    WS_KEY_PATH='ws_data/workstation-key'
    
    
    private 
    def get_internal(prop) 
      val = @provisioningConfig[prop]
      val = @system[prop] unless val # provisioningConfig can be used to override system config     
      return val
    end
    
    public
    attr_accessor :buildlabel
    attr_accessor :vms
    attr_accessor :sshcmd
    attr_accessor :scpcmd
    attr_accessor :knife_disable_hostkey_verification
    attr_accessor :system
    attr_accessor :provisioningConfig
    
    def initialize(deplconfig, label, start_time)
      @provisioningConfig = deplconfig[:provisioning_config]
      @system = deplconfig[:system]
      @vms = deplconfig[:vms]
      @envConfig = deplconfig
      @buildlabel = label
      @start_time = start_time
      @start_time_str = start_time.strftime("%Y%m%d%H%M%S")
      @deplconfig= deplconfig
      setdefaults
    end  
    
    
    # Get the environment configuration.
    def getEnvironmentConfiguration()
        return @deplconfig
    end
  
    def setdefaults
      @sshopts = get(:sshopts)
      @scpopts = get(:scpopts)

      @sshcmd = "ssh #{@sshopts}"
      @scpcmd = "scp #{@scpopts}"
      
      @knife_disable_hostkey_verification = get(:knife_disable_hostkey_verification)
      
    end
    
    def regenerateConfigObject()
         configObj= Hash.new        
         configObj[:provisioning_config]=@provisioningConfig
         configObj[:system]=@system
         configObj[:vms]=@vms
         return configObj
    end
    
    def get(prop)
    
      if prop.to_sym == :vms
          return @vms
      end
            
      val = get_internal(prop.to_sym)
      val = get_internal(prop.to_s) unless val
      return val
    end
    
    def wsKeyFile
      return WS_KEY_PATH
    end
    
    def wsPubKeyFile
      return WS_PUB_KEY_PATH
    end
    
    def [](key)
      return get(key)
    end
    
    def sshopts
      return @sshopts ? @sshopts : ""
    end
    
    def scpopts
      return @scpopts ? @scpopts : ""
    end
    
    def getNodeConfigurationByRole(role)
    
           roleConfig = nil
           @vms.each do | _roleConfig|
                  if _roleConfig[Depac::CloudDeploymentConstants::ROLE].eql? role
                        roleConfig = _roleConfig
                  end 
           end        
           return roleConfig
    end
    
    # Get the softlayer credentials.
    def getCloudCredentials()
          
          apikeyfile = File.expand_path(@envConfig['apikeyloc']) if @envConfig['apikeyloc']
          apikeyfile = File.expand_path(@envConfig[:system]['apikeyloc']) if apikeyfile == nil   

          if apikeyfile == nil
               Depac::Log.fatal_exit("'apikeyloc' needs to be specified in the deployment configuration file")
          end

          Depac::Log.fatal_exit("'apikeyloc' file was not found. Configured value: #{apikeyfile}") unless File.exists?(apikeyfile)
          return YAML.load_file(apikeyfile)
    end
    
  end
  
end
end